﻿using LxBasic;
using LxCore;
using System.Diagnostics.Contracts;

namespace LxCore
{
    public class InitialValue : JsObject
    {
        public FieldBase<double> InitialTemperature
        {
            get => _InitialTemperature;
            set { _InitialTemperature = value; RaisePropertyChanged(nameof(InitialTemperature)); }
        }
        private FieldBase<double> _InitialTemperature = new ConstField.Double(300);
        public FieldBase<Vector3d> InitialVelocity
        {
            get => _InitialVelocity;
            set { _InitialVelocity = value; RaisePropertyChanged(nameof(InitialVelocity)); }
        }
        private FieldBase<Vector3d> _InitialVelocity = ConstField.Vector0;
        public FieldBase<double> InitialPressure
        {
            get => _InitialPressure;
            set { _InitialPressure = value; RaisePropertyChanged(nameof(InitialPressure)); }
        }
        private FieldBase<double> _InitialPressure = ConstField.Double0;
        public FieldBase<double> InitialTurbIntensity
        {
            get => _InitialTurbIntensity;
            set { _InitialTurbIntensity = value; RaisePropertyChanged(nameof(InitialTurbIntensity)); }
        }
        private FieldBase<double> _InitialTurbIntensity = new ConstField.Double(0.01);
        public FieldBase<double> InitialTurbVelRef
        {
            get => _InitialTurbVelRef;
            set { _InitialTurbVelRef = value; RaisePropertyChanged(nameof(InitialTurbVelRef)); }
        }
        private FieldBase<double> _InitialTurbVelRef = new ConstField.Double(1);
        public FieldBase<double> InitialTurbVisRate
        {
            get => _InitialTurbVisRate;
            set { _InitialTurbVisRate = value; RaisePropertyChanged(nameof(InitialTurbVisRate)); }
        }
        private FieldBase<double> _InitialTurbVisRate = new ConstField.Double(10);

        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(InitialTemperature): jsReader.ReadObject(InitialTemperature, e => InitialTemperature = e); break;
                case nameof(InitialVelocity): jsReader.ReadObject(InitialVelocity, e => InitialVelocity = e); break;
                case nameof(InitialPressure): jsReader.ReadObject(InitialPressure, e => InitialPressure = e); break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter jsWriter)
        {
            base.ToJsonKV(jsWriter);
            var pm = Parent as PhysicalModel;
            if (pm.IsEnergyEnabled)
            {
                jsWriter.WriteObject(nameof(InitialTemperature), InitialTemperature, true);
            }
            if (pm.FlowType != null)
            {
                jsWriter.WriteObject(nameof(InitialVelocity), InitialVelocity, true);
                jsWriter.WriteObject(nameof(InitialPressure), InitialPressure, true);
            }
        }
    }
}
